package com.qing.demo.netty.util;


import cn.hutool.json.JSONUtil;
import com.qing.demo.netty.domain.ServerMsgProtocol;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.w3c.dom.Text;

/**
 * @author caozhipeng
 * @date 2025/7/29
 */
public class MsgUtil {

    public static TextWebSocketFrame buildMsgAll(String channelId, String msgInfo){
//        模拟头像
        int i=Math.abs(channelId.hashCode())%10;

        ServerMsgProtocol msgProtocol = new ServerMsgProtocol();
        msgProtocol.setType(2); //群发消息
        msgProtocol.setChannelId(channelId);
        msgProtocol.setUserHeadImg("head"+i+".jpg");
        msgProtocol.setMsgInfo(msgInfo);
        return new TextWebSocketFrame(JSONUtil.toJsonPrettyStr(msgProtocol));
    }

    public static TextWebSocketFrame buildMsgOwner(String channelId){
        ServerMsgProtocol msgProtocol = new ServerMsgProtocol();
        msgProtocol.setType(1); //1 链接信息 2：消息信息
        msgProtocol.setChannelId(channelId);
        return new TextWebSocketFrame(JSONUtil.toJsonPrettyStr(msgProtocol));
    }
}
